#!/usr/local/bin/bash
#Written by:
# - Igor Rafael [igor@dcc.ufmg.br]
# - Pericles Alves [periclesrafael@dcc.ufmg.br]

#########################################################################
#                                                                       #
#   - To execute:                                                       #
#       ./experiments <shell> <#_of_executions>                         #
#                                                                       #
#   - You need GNUPlot installed                                        #
#                                                                       #
#   - To disable a test file, put a # before its name in the LIST file  #
#                                                                       #
#########################################################################

SUITE_PATH=.
SHELL=$1
ANALYSER_PATH=$SUITE_PATH/analyser.py

DISCARD_TIMES=4
RUN_TIMES=$2

OUTPUT_PATH=/tmp/cp_abcp
OUTPUT_PREFIX=time
RESULTS_SUFFIX=speedup_results
PARAM_SUFFIX="-n --ion"

declare -A PARAMS
PARAMS["base"]="--ion-gvn=off --ion-abcp=off --ion-cp=off" 
PARAMS["cp"]="--ion-cp=on --ion-abcp=off --ion-gvn=off" 
PARAMS["abcp"]="--ion-cp=off --ion-abcp=on --ion-gvn=off" 
PARAMS["gvn"]="" 
PARAMS["abgvn"]="--ion-abgvn=on"
BASELINE="base"

function countActiveTests {
    TOTAL=0
    ENABLED=0

    for TEST_FILE in `cat $SUITE_PATH/LIST`; do  
        TOTAL=`expr $TOTAL + 1` ;
    done

    for TEST_FILE in `cat $SUITE_PATH/LIST | grep -v "^#"`; do  
        ENABLED=`expr $ENABLED + 1` ;
    done
    
    echo ""
    echo "Active tests: $ENABLED/$TOTAL"
    echo ""
}

# $1 name of the version running: string
# $2 IM params to run: string
function discardFirstRuns {
    echo "" 
    echo "Discarding the first $DISCARD_TIMES executions of the $1 version" 
    echo ""

    for((j=0; j<$DISCARD_TIMES; j++))
    do
        echo "$j"   

        for TEST_FILE in `cat $SUITE_PATH/LIST | grep -v "^#"`; do  
            $SHELL -b $2 $SUITE_PATH/$TEST_FILE
        done
    done
}

# $1 name of the version running: string
# $2 IM params to run: string
# $3 output file: string
function runBenchmark {
    echo ""
    echo "Executing the $1 version $RUN_TIMES times"
    echo ""

    for((j=0; j<$RUN_TIMES; j++))
    do   
        for TEST_FILE in `cat $SUITE_PATH/LIST | grep -v "^#"`; do
            $SHELL -b $2 $SUITE_PATH/$TEST_FILE >> ${3}_${TEST_FILE#*/}
        done
    done
}

# $1 name of the version running: string
# $2 results file: string
function analyseSpeedUp {
    #extracting the speedup of the CP version
    echo "Resultfile=$2"
    for TEST_FILE in `cat $SUITE_PATH/LIST | grep -v "^#"`; do  
        python $ANALYSER_PATH $OUTPUT_PATH/${OUTPUT_PREFIX}_${BASELINE}_${TEST_FILE#*/} $OUTPUT_PATH/${OUTPUT_PREFIX}_${1}_${TEST_FILE#*/} $TEST_FILE >> $2
    done
}

#$1 name of a version: string
#$2 name of a version: string
#$3 outputfile: string
function plotFile {
	V1="$OUTPUT_PATH/${1}_${RESULTS_SUFFIX}"
	V2="$OUTPUT_PATH/${2}_${RESULTS_SUFFIX}"
	PLOT_FILE="$OUTPUT_PATH/plot_$3.gnu"
    echo "set terminal postscript eps enhanced color" >> $PLOT_FILE
    echo "set out '$OUTPUT_PATH/$3.eps'" >> $PLOT_FILE
	echo "set style fill solid" >> $PLOT_FILE
    echo "set yrange [-1:1]" >> $PLOT_FILE
    echo "set ylabel \"Speedup\"" >> $PLOT_FILE
    echo "set xlabel \"Benchmark\"" >> $PLOT_FILE 
    echo "set xtics rotate by 45 out offset 0,1" >> $PLOT_FILE
    echo "plot \"$V1\" using 2: xtic(1) with histogram title \"${1} speedup\",\"$V2\" using 2: xtic(1) with histogram \\" >> $PLOT_FILE   
    echo "title \"${2} speedup\"" >> $PLOT_FILE
    gnuplot $PLOT_FILE 
}

if [ -d $SUITE_PATH ];then
if [ -e $SHELL ];then
    #Discard old results
    rm -rf $OUTPUT_PATH
    mkdir $OUTPUT_PATH

    #Calculates the percentage of active tests
	countActiveTests

	for i in ${!PARAMS[@]}; do
		PARAM="$PARAM_SUFFIX ${PARAMS[$i]}"
		echo "Param=$PARAM"
		discardFirstRuns $i "$PARAM"
		OUTPUT="$OUTPUT_PATH/${OUTPUT_PREFIX}_${i}"
		runBenchmark $i "$PARAM" "$OUTPUT"
		echo ""
	done

	PARAM_KEYS=${!PARAMS[@]}
	for i in ${PARAM_KEYS[@]//$BASELINE/}; do
		RESULT="$OUTPUT_PATH/${i}_${RESULTS_SUFFIX}"
		analyseSpeedUp $i "$RESULT"
	done

    #prints the results
    echo ""
    echo "-------------------------------------------"
    echo "SPEEDUP RESULTS"
    echo "-------------------------------------------"
    echo ""

	#Graph 1: time comparison between CP vs ABCP, bars (GVN=off)
	plotFile "cp" "abcp" "cp_vs_abcp"
	#Graph 2: time comparison between GVN vs ABGVN, bars (cp=off)
	plotFile "gvn" "abgvn" "gvn_vs_abgvn"
	#Graph 3: time comparison between ABGVN vs ABCP (GVN=off), bars
	plotFile "abgvn" "abcp" "abgvn_vs_abcp"
	#Graph 4: 1: static results: folded instructions (CP, ABCP, GVN, ABGVN), bars
	
else
    echo "JS shell path not found"
    echo ""
fi
else
    echo "Test suite path not found"
    echo ""
fi 
